package com.fingard.dsp.bank.directbank.citic01;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.fingard.dsp.bank.directbank.bankInterface.IVirtualAccount;
import com.fingard.dsp.bank.reqrespobj.virtualaccount.Req7503BalQry;
import com.fingard.dsp.bank.reqrespobj.virtualaccount.RetAts7503BalQry;
import com.fingard.dsp.bank.reqrespobj.virtualaccount.detail.RetAtsOpenAccount;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CITIC01VirtualAccount extends CITIC01Base implements IVirtualAccount {
    //虚拟账户开户
    public RetAts7503BalQry d_7503_BalQry(Req7503BalQry p_req7503) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        //余额查询返回ATS的对象
        RetAts7503BalQry retAts = new RetAts7503BalQry(p_req7503.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            //赋值通用参数
            CITIC01Header bankHeader = new CITIC01Header();
            bankHeader.action = "DLBREGSN";

            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);

            xmlWriter.writeElementString("mainAccNo", p_req7503.actNum);
            xmlWriter.writeElementString("appFlag", "2");
            xmlWriter.writeElementString("accGenType", "0");
            xmlWriter.writeElementString("subAccNo", "");
            xmlWriter.writeElementString("subAccNm", p_req7503.actName);
            xmlWriter.writeElementString("accType", "03");
            xmlWriter.writeElementString("calInterestFlag", "0");
            xmlWriter.writeElementString("interestRate", "");
            xmlWriter.writeElementString("overFlag", "0");
            xmlWriter.writeElementString("overAmt", "");
            xmlWriter.writeElementString("overRate", "");
            xmlWriter.writeElementString("autoAssignInterestFlag", "0");
            xmlWriter.writeElementString("autoAssignTranFeeFlag", "0");
            xmlWriter.writeElementString("feeType", "0");
            xmlWriter.writeElementString("realNameParm", "0");
            xmlWriter.writeElementString("subAccPrintParm", "0");
            xmlWriter.writeElementString("mngNode", "111001");
            xmlWriter.writeElementString("vtlCustNm", "123");
            xmlWriter.writeElementString("legalPersonNm", "123");
            xmlWriter.writeElementString("custCertType", "5");
            xmlWriter.writeElementString("custCertNo", "123");
            xmlWriter.writeElementString("branch", "123");
            xmlWriter.writeElementString("commAddress", "123");
            xmlWriter.writeStartElementByAttr("list", "<list name=\"VilcstDataList\">");
            xmlWriter.writeStartElement("row");
            xmlWriter.writeElementString("contactName", sdf.format(new Date()));
            xmlWriter.writeElementString("contactPhone", sdf.format(new Date()));
            xmlWriter.writeElementString("mailAddress", "112233@qq.com");
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();

                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpStatus = tmpBkReader.getFirstText("/stream/status");
                    String tmpStatusText = tmpBkReader.getFirstText("/stream/statusText");
                    if (tmpStatus != null && tmpStatus.equals("AAAAAAA")) {
                        RetAtsOpenAccount tmpRetDetail = new RetAtsOpenAccount();
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.subAccNo = tmpBkReader.getFirstText("/stream/subAccNo");
                        tmpRetDetail.subAccNm = tmpBkReader.getFirstText("/stream/subAccNm");
                        tmpRetDetail.hostNo = tmpBkReader.getFirstText("/stream/hostNo");

                        retAts.writeRetDetail(tmpRetDetail);
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpStatusText);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }
}
